package com.gitlab.lucky.eleme.service;

import com.github.pagehelper.PageInfo;
import com.gitlab.lucky.common.dao.mapper.LuckyRecordMapper;
import com.gitlab.lucky.common.dao.model.LuckyRecord;
import com.gitlab.lucky.common.dao.model.WeixinGzhUser;
import com.gitlab.lucky.common.enums.Constant;
import com.gitlab.lucky.common.service.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDate;
import java.util.List;

/**
 * @program: lucky
 * @author: Mr.Han
 * @create: 2019-02-26 14:02
 **/
@Service
public class LuckyRecordService extends ServiceImpl<LuckyRecordMapper, LuckyRecord> {
    @Autowired
    private LuckyRecordMapper luckyRecordMapper;
    public static final Logger LOGGER = LoggerFactory.getLogger(LuckyRecordService.class);

    /**
     * 查询今天手气最佳没有被领取的记录，且次刷新数小于8次的
     *
     * @return
     */
    public List<LuckyRecord> selectByNow() {
        Example example = new Example(LuckyRecord.class);
        LocalDate now = LocalDate.now();
        LocalDate plus = now.plusDays(1);
        example.createCriteria()
                .andBetween("createTime", now.toString(), plus.toString())
                .andIsNotNull("ckSid")
                .andIsNull("luckyMoney")
                .andLessThan("count", Constant.LUCKY_MIN_COUNT);
        List<LuckyRecord> luckyRecords = this.selectByExample(example);
        return luckyRecords;
    }

    /**
     * 查询所有刷新超过8次的记录
     *
     * @return
     */
    public LuckyRecord selectPwd() {
        Example example = new Example(LuckyRecord.class);
        example.createCriteria()
                .andGreaterThanOrEqualTo("count", Constant.LUCKY_MAX_COUNT)
                .andIsNull("luckyMoney");
        List<LuckyRecord> luckyRecords = this.selectByExample(example);
        if (luckyRecords != null && luckyRecords.get(0) != null) {
            LuckyRecord luckyRecord = luckyRecords.get(0);
            this.deleteByPrimaryKey(luckyRecords.get(0).getId());
            return luckyRecord;
        }
        return null;
    }

    /**
     * 查询所有刷新超过8次的记录
     *
     * @return
     */
    public List<LuckyRecord> selectLucky() {
        Example example = new Example(LuckyRecord.class);
        example.createCriteria()
                .andGreaterThanOrEqualTo("count", Constant.LUCKY_MIN_COUNT)
                .andIsNull("luckyMoney")
                .andLessThanOrEqualTo("count", Constant.LUCKY_MAX_COUNT);
        List<LuckyRecord> luckyRecords = this.selectByExample(example);
        LOGGER.info("没人要的红包数量为:{}", luckyRecords.size());
        return luckyRecords;
    }

    /**
     * 查询我的领取记录
     *
     * @param openId
     * @param platform
     */
    public PageInfo<LuckyRecord> selectLuckyRecordByOpenId(Integer pageNum,Integer pageSize,String openId, Integer platform) {
        Example example = new Example(LuckyRecord.class);
        example.createCriteria()
                .andEqualTo("openid", openId)
                .andEqualTo("platform", platform);
        example.orderBy("createTime").desc();
        PageInfo<LuckyRecord> luckyRecords = this.selectPage(pageNum,pageSize,example);
        return luckyRecords;
    }

    /**
     * 查询一共优惠金额
     * @param openId
     * @param platform
     * @return
     */
    public Double selectLuckyRecordMoneyByOpenId(String openId, Integer platform) {
        Double money = luckyRecordMapper.selectLuckyRecordMoneyByOpenId(openId, platform);
        if(money==null){
            money=0.0;
            return money;
        }
        String format = String.format("%.1f", money);
        money=Double.parseDouble(format);
        return money;
    }

    public Integer selectLuckyRecordCountByPlaform(Integer platform) {
        return luckyRecordMapper.selectLuckyRecordCountByPlaform(platform);
    }
}
